﻿using System;
using System.Web.UI.WebControls;
using System.Drawing;

namespace PlusMinus
{
    public class PlusMinusButton : Panel
    {
        public delegate void ClickEventHandler(object sender, EventArgs e);
        public event ClickEventHandler ClickPlus;
        public event ClickEventHandler ClickMinus;
        
        protected Button btnPlus;
        protected Button btnMinus;

        public int value { get { return ViewState["value"] == null ? 0 : (int)ViewState["value"]; }
            set 
            {
                ViewState["value"] = value;
                if (value == 1)
                {
                    this.btnPlus.BackColor = Color.Wheat;
                    this.btnMinus.BackColor = SystemColors.ButtonFace;
                }
                else if (value == -1)
                {
                    this.btnMinus.BackColor = Color.Wheat;
                    this.btnPlus.BackColor = SystemColors.ButtonFace;
                }
                else
                {
                    this.btnMinus.BackColor = SystemColors.ButtonFace;
                    this.btnPlus.BackColor = SystemColors.ButtonFace;
                }
            }
        }

        public string CommandArgument { get { return ViewState["arg"] == null ? "" : ViewState["arg"].ToString(); } set { ViewState["arg"] = value; } }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.btnPlus = new Button();
            this.btnPlus.Text = "+";
            this.btnPlus.Click += new System.EventHandler(btnPlus_Click);

            this.btnMinus = new Button();
            this.btnMinus.Text = "-";
            this.btnMinus.Click += new System.EventHandler(btnMinus_Click);

            this.Controls.Add(this.btnPlus);
            this.Controls.Add(this.btnMinus);
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            this.btnMinus.BackColor = Color.Wheat;
            this.btnPlus.BackColor = SystemColors.ButtonFace;
            this.value = -1;
            this.onClickMinus(e);
        }

        public void onClickMinus(EventArgs e)
        {
            this.ClickMinus(this, e);
        }

        private void btnPlus_Click(object sender, System.EventArgs e)
        {
            this.btnPlus.BackColor = Color.Wheat;
            this.btnMinus.BackColor = SystemColors.ButtonFace;
            this.value = 1;
            this.onClickPlus(e);
        }

        public void onClickPlus(EventArgs e)
        {
            this.ClickPlus(this, e);
        }
    }
}
